<script setup>
import { ref, onUnmounted } from 'vue';

const result = ref(0);
let worker;

const startWorker = () => {
  worker = new Worker('/worker.js'); // 路径根据实际位置调整
  worker.postMessage(10); // 发送数据
  worker.onmessage = (e) => result.value = e.data; // 接收结果
  worker.onerror = (e) => console.error('Worker error:', e);
};

onUnmounted(() => worker?.terminate()); // 组件卸载时销毁Worker
</script>

<template>
  <button @click="startWorker">启动Worker</button>
  <p>结果: {{ result }}</p>
</template>
